/**
 * @Subject.as
 *
 * @author sodaChen mail:asframe#163.com
 * @version 1.0
 * <br>Copyright (C), 2010 asFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame
 * <br>Date:2012-4-12
 */
package com.asframe.pattern.observer
{
	import com.asframe.utils.VectorUtils;

	/**
	 * 主题的默认实现对象
	 * @author sodaChen
	 * Date:2012-4-12
	 */
	public class Subject implements ISubject
	{
		private var mObserverList		:Vector.<IObserver>;
		private var len					:int;
		private var mNotcie				:*;

		public function Subject()
		{
			mObserverList = new Vector.<IObserver>();
		}
		public function setNotice(notice:*):void
		{
			this.mNotcie = mNotcie;
		}
		public function length():int
		{
			return mObserverList.length;
		}
		public function addObserver(observer:IObserver):void
		{
			if(observer == null)
				throw new Error("观察者对象不能为空!");
			//不能重复添加
			if(mObserverList.indexOf(observer) != -1)
				return ;
			mObserverList.push(observer);
		}

		public function removeObserver(observer:IObserver):void
		{
			if(observer == null)
			{
				throw new Error("观察者对象不能为空!");
			}
			VectorUtils.removeItem(mObserverList,observer);
		}

		public function send(...args):void
		{
			for each (var observer:IObserver in mObserverList)
			{
				if(mNotcie is String && observer["hasOwnProperty"](mNotcie))
				{
					observer[mNotcie].apply(observer,args);
				}
				else
				{
					observer.update(args);
				}
			}
		}
	}
}
